﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;

namespace HongLingJin
{
    public class ReadIDCard
    {
        [DllImport("Sdtapi.dll")]
        private static extern int InitComm(int iPort);//初始化

        [DllImport("Sdtapi.dll")]
        private static extern int CloseComm();//关闭端口

        [DllImport("Sdtapi.dll")]
        private static extern int Authenticate();//卡认证

        [DllImport("Sdtapi.dll")]//读取数据，推荐使用
        private static extern int ReadBaseInfos(
                                    StringBuilder Name,
                                    StringBuilder Gender,
                                    StringBuilder Folk,
                                    StringBuilder BirthDay,
                                    StringBuilder Code,
                                    StringBuilder Address,
                                    StringBuilder Agency,
                                    StringBuilder ExpireStart,
                                    StringBuilder ExpireEnd);

        [DllImport("kernel32.dll")]
        private static extern int Beep(int dwFreq, int dwDuration);//用来大吼一声

        public event Action<IDCardInfo> IDCardInfoCompleted;
        public event Action<string> OnError;

        //enum ERetReadBaseInfos
        //{
        //    正确 = 1,
        //    读卡错误 = 0,
        //    没有最新住址信息 = 2,
        //    相片解码错误 = -1,
        //    wlt文件后缀错误 = -2,
        //    wlt文件打开错误 = -3,
        //    wlt文件格式错误 = -4,
        //    软件未授权 = -5,
        //    无效参数 = -11,
        //}


        public void ReadCard()
        {
            StringBuilder Name = new StringBuilder(31);
            StringBuilder Gender = new StringBuilder(3);
            StringBuilder Folk = new StringBuilder(10);
            StringBuilder BirthDay = new StringBuilder(9);
            StringBuilder Code = new StringBuilder(19);
            StringBuilder Address = new StringBuilder(71);
            StringBuilder Agency = new StringBuilder(31);
            StringBuilder ExpireStart = new StringBuilder(9);
            StringBuilder ExpireEnd = new StringBuilder(9);

            //打开端口
            int intOpenRet = 0;
            for (int i = 1001; i < 1004; i++)
            {
                intOpenRet = InitComm(i);

                if (intOpenRet == 1)
                    break;

            }
            if (intOpenRet != 1)
            {
                if (OnError != null) OnError("阅读机具未连接");
                return;
            }
            //卡认证
            int intReadRet = Authenticate();
            if (intReadRet != 1)
            {
                if (OnError != null) OnError("卡认证失败");

                CloseComm();
                return;
            }

            //ReadBaseInfos（推荐使用）
            int intReadBaseInfosRet = ReadBaseInfos(
                                        Name, Gender, Folk, BirthDay,
                                        Code, Address, Agency,
                                        ExpireStart, ExpireEnd);

            if (intReadBaseInfosRet == 0)
            {
                if (OnError != null) OnError("读卡错误");

                CloseComm();
                return;
            }

            //Beep(2047, 200);
            IDCardInfo IDInfo = new IDCardInfo();
            IDInfo.Code = Code.ToString().Trim();
            IDInfo.Name = Name.ToString().Trim();
            IDInfo.Gender = Gender.ToString().Trim();
            IDInfo.Folk = Folk.ToString().Trim();
            IDInfo.BirthDay = BirthDay.ToString().Trim();
            IDInfo.Address = Address.ToString().Trim();
            IDInfo.Agency = Agency.ToString().Trim();
            IDInfo.ExpireStart = ExpireStart.ToString().Trim();
            IDInfo.ExpireEnd = ExpireEnd.ToString().Trim();
            IDInfo.Photo = Directory.GetCurrentDirectory() + "\\photo.bmp";

            CloseComm();

            if (IDCardInfoCompleted != null)
            {
                this.IDCardInfoCompleted(IDInfo);
            }


        }

    }

    public class IDCardInfo
    {
        public String Name { get; set; }

        public String Gender { get; set; }

        public String Folk { get; set; }

        public String BirthDay { get; set; }

        public String Code { get; set; }

        public String Address { get; set; }

        public String Agency { get; set; }

        public String ExpireStart { get; set; }

        public String ExpireEnd { get; set; }

        public String Photo { get; set; }
    }
}
